import * as types from '../mutation-types'

const state = {
  hearAnswer: JSON.parse(localStorage.getItem('hearAnswer')) || {},  //听力题 做题记录
  hearReply: JSON.parse(localStorage.getItem('hearReply')) || {},  //听力简答 做题记录
  hearFill: JSON.parse(localStorage.getItem('hearFill')) || {},  //听力简答 做题记录
}

const actions = {
  // 听力题 做题记录
  hearAnswer({ commit }, res) {
    localStorage.setItem('hearAnswer', JSON.stringify(res))
    commit(types.CON_HEAR_ANSWER, res)
  },

  // 清除 听力题 做题记录
  clearHear({ commit }) {
    localStorage.removeItem('hearAnswer')
    commit(types.CON_HEAR_ANSWER, {})
  },

  // 听力简答 做题记录
  hearReply({ commit }, res) {
    localStorage.setItem('hearReply', JSON.stringify(res))
    commit(types.CON_HEAR_REPLY, res)
  },

  // 清除 听力简答 做题记录
  clearReply({ commit }) {
    localStorage.removeItem('hearReply')
    commit(types.CON_HEAR_REPLY, {})
  },

  // 听力填空 做题记录
  hearFill({ commit }, res) {
    localStorage.setItem('hearFill', JSON.stringify(res))
    commit(types.CON_HEAR_FILL, res)
  },

  // 清除 听力填空 做题记录
  clearBlanks({ commit }) {
    localStorage.removeItem('hearFill')
    commit(types.CON_HEAR_FILL, {})
  },


}

const getters = {
  hearAnswer: state => state.hearAnswer,
  hearReply: state => state.hearReply,
  hearFill: state => state.hearFill,
}

const mutations = {
  [types.CON_HEAR_ANSWER](state, res) {
    state.hearAnswer = res
  },

  [types.CON_HEAR_REPLY](state, res) {
    state.hearReply = res
  },

  [types.CON_HEAR_FILL](state, res) {
    state.hearFill = res
  },
}

export default {
  state,
  actions,
  getters,
  mutations
}
